package com.xiaoyu.tool.messageQueue.receiver;

import com.xiaoyu.tool.entity.order.Order;
import com.xiaoyu.tool.enums.order.OrderStatus;
import com.xiaoyu.tool.service.order.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * rabbitmq配置
 * */
@Slf4j
@Component
@RabbitListener(queues = "expireAndDeleteOrderDelayQueue")
public class ExpireAndDeleteOrderHandlerReceiver {

    private final OrderService orderService;

    public ExpireAndDeleteOrderHandlerReceiver(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 检查订单是否过期，需要取消
     *
     * @param orderNo
     */
    @RabbitHandler
    public void checkOrderExpiredAndDelete(String orderNo) {
        Order order = orderService.findOrderByOrderNo(orderNo);
        if(order != null && order.getStatus().equals(OrderStatus.等待支付)){
            orderService.loseOrder(orderNo);
        }
        log.info("消费者:检查订单是否过期,需要取消:orderNo = {}",orderNo);
    }
}
